//
//  NSString+DateString.m
//  wentong
//
//  Created by xieyingze on 15/12/17.
//  Copyright © 2015年 xieyingze. All rights reserved.
//

#import "NSString+DateString.h"

@implementation NSString (DateString)


/**
 已转化为东八区显示时间

 @param date 时间戳
 @param formatter 时间显示格式
 @return 时间字符串显示格式
 */
+ (instancetype)dateStringWithDate:(NSDate *)date AndDateFormatter:(NSString *)formatter
{
    NSDateFormatter *forma = [[NSDateFormatter alloc]init];
    forma.timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
    forma.dateFormat = formatter;
    return [forma stringFromDate:date];
}

+ (instancetype)dateStringWithDate:(NSDate *)date AndDateFormatter:(NSString *)formatter isConversionTimeZone:(BOOL)isConversion{
    date = isConversion ? [date dateByAddingTimeInterval:[NSTimeZone localTimeZone].secondsFromGMT] : date;//时区转换
    NSDateFormatter *forma = [[NSDateFormatter alloc]init];
    forma.timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
    forma.dateFormat = formatter;
    return [forma stringFromDate:date];
}

+ (instancetype)dateStringWithTime:(NSTimeInterval)timeTnterval AndDateFormatter:(NSString *)formatter isConversionTimeZone:(BOOL)isConversion{
    if (timeTnterval > 1000000000000) timeTnterval = timeTnterval/1000;//时间戳现阶段为10位数，带毫秒为13位
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeTnterval];
    return [NSString dateStringWithDate:date AndDateFormatter:formatter isConversionTimeZone:isConversion];
}

- (NSDate *)transformToDateWithFormatter:(NSString *)formatter {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:formatter];
    return [dateFormatter dateFromString:self];
}

/**
 获取当前的时间戳
 
 @return 当前时间戳
 */
+ (NSString *)getCurrentTimestamp
{
    NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
    NSTimeInterval a=[dat timeIntervalSince1970];
    NSString*timeString = [NSString stringWithFormat:@"%0.f", a];//转为字符型
    return timeString;
}

+ (NSInteger)getCurrentMonth
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    //获取当前年份, 月份, 号数
    unsigned unitFlags = NSYearCalendarUnit |NSMonthCalendarUnit |NSDayCalendarUnit;
    NSDateComponents *components = [calendar components:unitFlags fromDate:[NSDate date]];
    //NSLog(@"%ld, %ld, %ld", (long)components.year, (long)components.month, (long)components.day);
    return components.month;
}

+ (NSInteger)getCurrentDay
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    //获取当前年份, 月份, 号数
    unsigned unitFlags = NSYearCalendarUnit |NSMonthCalendarUnit |NSDayCalendarUnit;
    NSDateComponents *components = [calendar components:unitFlags fromDate:[NSDate date]];
    //NSLog(@"%ld, %ld, %ld", (long)components.year, (long)components.month, (long)components.day);
    return components.day;
}

+ (NSUInteger)getCurrentMonthDays
{
    //1.获取当月的总天数
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSRange range = [calendar rangeOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:[NSDate date]];
    NSUInteger numberOfDaysInMonth = range.length;
    return numberOfDaysInMonth;
}

+ (NSString *)getCurrentWeekDay
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    //获取当前日期星期几
    NSArray *weekdays = [NSArray arrayWithObjects:@"周日", @"周一", @"周二", @"周三", @"周四", @"周五", @"周六",nil];
    NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Shanghai"];
    [calendar setTimeZone: timeZone];
    NSDate *date = [NSDate date];
    NSCalendarUnit calendarUnit = NSWeekdayCalendarUnit;
    NSDateComponents *theComponents = [calendar components:calendarUnit fromDate:date];
    return [weekdays objectAtIndex:theComponents.weekday - 1];
}

+ (NSInteger *)getCurrentWeekDayIndex
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    //获取当前日期星期几
    NSArray *weekdays = [NSArray arrayWithObjects:@"周日", @"周一", @"周二", @"周三", @"周四", @"周五", @"周六",nil];
    NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Shanghai"];
    [calendar setTimeZone: timeZone];
    NSDate *date = [NSDate date];
    NSCalendarUnit calendarUnit = NSWeekdayCalendarUnit;
    NSDateComponents *theComponents = [calendar components:calendarUnit fromDate:date];
    return theComponents.weekday;
}


/**
 获取时间字符串具体字段

 时间字符串格式为yyyyMMddHHmmss
 @return 年、月、日、时、分、秒
 */
- (NSString *)getTimeStringYear
{
    NSString *year = [self substringWithRange:NSMakeRange(0, 4)];
    return year;
}

- (NSString *)getTimeStringMonth
{
    NSString *month = [self substringWithRange:NSMakeRange(4, 2)];
    return month;
}

- (NSString *)getTimeStringDay
{
    NSString *day = [self substringWithRange:NSMakeRange(6, 2)];
    return day;
}

- (NSString *)getTimeStringHour
{
    NSString *hour = [self substringWithRange:NSMakeRange(8, 2)];
    return hour;
}

- (NSString *)getTimeStringMinute
{
    NSString *minute = [self substringWithRange:NSMakeRange(10, 2)];
    return minute;
}

- (NSString *)getTimeStringSencond
{
    NSString *second = [self substringWithRange:NSMakeRange(12, 2)];
    return second;
}
@end
